/************************************  

DESCRIPTION: This program estimates the blocking estimator for men

Note: Implementation of this program ran some of the block estimates in chunks.
	We use 3a.Blocking_Male_combined to put the estimates together.  

Datasets Used:
 (1) maleAllBlocks
 (2) maleDiabBlocks
 (3) maleHypBlocks
 (4) maleLPManalysis2

Datasets Created:
 (1) malebetaBlockDiab
	(malebetaBlockDiab_lt15 and malebetaBlockDiab_gt15)
 (2) malebetaBlockHyp
 (3) malebetaBlockDeath 
 
************************************/  

set more off
capture log close
clear all

set matsize 10000

global base "\\192.168.6.107\silstorage"
*global base "N:"
cd "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\Blocking"

global origData "$base\MedicareClaims-P045601-BE"
*Use data from LPM
global dataIn "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\LPM"
global dataOut "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "$base\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "$base\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "$base\MedicareClaims-P045601-BE\Work\ska"
global output

adopath +  "$base/SIL-Common/estout"
adopath +  "$base/SIL-Common/outreg2"
adopath +  "$base/SIL-Common/reghdfe-master/package"

log using "$logs/2ahyp.BlockingEstimator_MaleRegression.log", replace


*Columnames program

	program getColnames 
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local names : colfullnames A

	 svmat A, names(col)

	 gen rownames = ""
	 forvalues i = 1/`c' {

	  replace rownames = "`:word `i' of `rownames''" in `i' 
	   } 

	 }

	end


	program getRegStats
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local n = `c' + 1
	 local rsq = `c' + 3

	 replace beta1 =e(N) in `n' 
	 replace beta1 =e(r2_p) in `rsq' 

	 }

	end

*Setup block data
*Will merge this information onto the claims data
* don't want to overwrite the vmerger variable on that dataset
	*All
	use ./maleAllBlocks.dta
		 sort BENE_ID 
		 capture rename vmerger VMERGER
	 save ./maleAllBlocks.dta, replace
	 clear
 
	*Diabetes
	 use ./maleDiabBlocks.dta
		 sort BENE_ID 
		 capture rename vmerger VMERGER
	 save ./maleDiabBlocks.dta, replace
	 clear
 
	*Hypertension
	 use ./maleHypBlocks.dta
		 sort BENE_ID 
		 capture rename vmerger VMERGER
	 save ./maleHypBlocks.dta, replace
	 clear

	 *Readin claims data
	 use $dataIn/maleLPManalysis2.dta
	 *Clean claims data
	 *drop contradictions and missing data
	  drop if mxhypertension_p_all==1 & mxicd8_chronic_p_all==0
	  drop if ehat ==.
	  keep if mxdiabetes_p_all ==1
	  capture drop urban_rural
		  gen urban_rural=urbanrural1+2*urbanrural2
		   replace urban_rural=3 if urban_rural==0
		  capture rename mxicd8_chronic mxicd8_cardiac
	  capture drop spec35 specrt spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
	  capture drop _merge
		  gen zip3 = substr(zip_pat,1,3)   
		  encode zip3, gen(zip3n)	 
	 
*DIABETES
	sort BENE_ID
	merge m:1 BENE_ID using ./maleDiabBlocks.dta
	  drop EHAT VMERGER
	 levelsof block , local(lblock)
	 
	 foreach b of local lblock {
		 foreach var of varlist oc_diabcomp2 oc_diabcomp1 oc_glaucoma oc_death {
			 if regexm("`var'","oc_([a-z0-9]+)$") { 
			  local pre = "`=regexs(1)'"
			 }
		 
		 di "`pre'" " " "`b'"
		 
		 capture rename mxicd3_chronic mxicd3_endocrine 
		 
		areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp nptf anesth spec* fs* lnvisit* if mxdiabetes_p_all==1 & block==`b' & kv_`pre'==1, abs(zipwhite) cluster(zip3)   

		  matrix beta  = e(b)' 
		  matrix sesq = vecdiag(e(V))' 
		  svmat beta
		  getColnames
		  getRegStats
		  svmat sesq
		  rename rownames rn`pre'_`b'
		  capture drop y1 
		  capture drop j 
		  capture drop i
		  rename beta1 beta_`pre'_`b'
		  rename sesq1 sesq_`pre'_`b'
		 
		 
		  matrix B = e(b)
		  local nvars = colsof(B)
		 
		  replace rn`pre'_`b' = "N" if _n == `nvars' + 1
		  replace beta_`pre'_`b' = e(N) if _n == `nvars' + 1
		 
		  replace rn`pre'_`b' = "R2" if _n == `nvars' + 2
		  replace beta_`pre'_`b' = e(r2) if _n == `nvars' + 2
		 
		  replace rn`pre'_`b' = "aR2" if _n == `nvars' + 3
		  replace beta_`pre'_`b' = e(r2_a) if _n == `nvars' + 3
		 
		  }
	 }
	 
	 keep rn* beta* sesq*
	 drop if _n>1000
	 save ./malebetaBlockDiab.dta, replace
	 clear
 

 *HYPERTENSION
	use $dataIn/maleLPManalysis2.dta
	 *Drop these contradictions
	  drop if mxhypertension_p_all==1 & mxicd8_chronic_p_all==0
	 *hypertension
	  keep if mxhypertension_p_all ==1
	 drop if ehat ==.
	  capture drop urban_rural
	  gen urban_rural=urbanrural1+2*urbanrural2
	  replace urban_rural=3 if urban_rural==0
	  capture rename mxicd8_chronic mxicd8_cardiac
	  capture drop spec35 specrt spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
	  capture drop _merge
	  gen zip3 = substr(zip_pat,1,3)   
	  encode zip3, gen(zip3n)	 

  
	sort BENE_ID
	merge m:1 BENE_ID using ./maleHypBlocks.dta
	  drop ehat VMERGER
	 
	 levelsof block  , local(lblock)
	 foreach b of local lblock {
	*Hypertension 
		 foreach var of varlist oc_acuteicd8 oc_ami oc_ihd oc_death {
			 if regexm("`var'","oc_([a-z0-9]+)$") { 
				  local pre = "`=regexs(1)'"
			 }
		 
		 di "`pre'" " " "`b'"
		 
		 capture rename mxicd3_endocrine mxicd3_chronic 
		 capture rename mxicd8_chronic mxicd8_cardiac
		 
		areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_ch* fmp nptf anesth spec* fs* lnvisit* if mxdiabetes_p_all==1 & block==`b' & kv_`pre'==1, abs(zipwhite) cluster(zip3)   

		  matrix beta  = e(b)' 
		  matrix sesq = vecdiag(e(V))' 
		  svmat beta
		  getColnames
		  getRegStats
		  svmat sesq
		  rename rownames rn`pre'_`b'
		  capture drop y1 
		  capture drop j 
		  capture drop i
		  rename beta1 beta_`pre'_`b'
		  rename sesq1 sesq_`pre'_`b'
		 
		 
		  matrix B = e(b)
		  local nvars = colsof(B)
		 
		  replace rn`pre'_`b' = "N" if _n == `nvars' + 1
		  replace beta_`pre'_`b' = e(N) if _n == `nvars' + 1
		 
		  replace rn`pre'_`b' = "R2" if _n == `nvars' + 2
		  replace beta_`pre'_`b' = e(r2) if _n == `nvars' + 2
		 
		  replace rn`pre'_`b' = "aR2" if _n == `nvars' + 3
		  replace beta_`pre'_`b' = e(r2_a) if _n == `nvars' + 3
		 
		  
		  }
		}
	 keep rn* beta* sesq*
	 drop if _n>1000
	 save ./malebetaBlockHyp.dta, replace
	 clear


	 *DEATH
	 use $dataIn/maleLPManalysis2.dta
	 *Drop these contradictions
	  drop if mxhypertension_p_all==1 & mxicd8_chronic_p_all==0
	  drop if ehat ==.
	  capture drop urban_rural
		  gen urban_rural=urbanrural1+2*urbanrural2
		   replace urban_rural=3 if urban_rural==0
		  capture rename mxicd8_chronic mxicd8_cardiac
	  capture drop spec35 specrt spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
	  capture drop _merge
		  gen zip3 = substr(zip_pat,1,3)   
		  encode zip3, gen(zip3n)	 

		 
	sort BENE_ID
	capture drop _merge
	merge m:1 BENE_ID using ./maleAllBlocks.dta
	  drop ehat VMERGER
	 
		 levelsof block , local(lblock)
		 
		 foreach b of local lblock {
		 
			 foreach var of varlist oc_death {
				 if regexm("`var'","oc_([a-z0-9]+)$") { 
				  local pre = "`=regexs(1)'"
				 }
			 
			 di "`pre'" " " "`b'"
			 
			 capture rename mxicd3_endocrine mxicd3_chronic 
			 capture rename mxicd8_chronic mxicd8_cardiac
			 
			areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp nptf anesth spec* fs* lnvisit* if mxdiabetes_p_all==1 & block==`b' & kv_`pre'==1, abs(zipwhite) cluster(zip3)   

			  matrix beta  = e(b)' 
			  matrix sesq = vecdiag(e(V))' 
			  svmat beta
			  getColnames
			  getRegStats
			  svmat sesq
			  rename rownames rn`pre'_`b'
			  capture drop y1 
			  capture drop j 
			  capture drop i
			  rename beta1 beta_`pre'_`b'
			  rename sesq1 sesq_`pre'_`b'
			 
			 
			  matrix B = e(b)
			  local nvars = colsof(B)
			 
			  replace rn`pre'_`b' = "N" if _n == `nvars' + 1
			  replace beta_`pre'_`b' = e(N) if _n == `nvars' + 1
			 
			  replace rn`pre'_`b' = "R2" if _n == `nvars' + 2
			  replace beta_`pre'_`b' = e(r2) if _n == `nvars' + 2
			 
			  replace rn`pre'_`b' = "aR2" if _n == `nvars' + 3
			  replace beta_`pre'_`b' = e(r2_a) if _n == `nvars' + 3
			  }
		}

	keep rn* beta* sesq*
	 drop if _n>1000
	 save ./malebetaBlockDeath.dta, replace
	 clear
	

 